var _queryMap;

/*获取URL参数的函数
Request={
    QueryString:function(item) {
        var svalue=location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
        return svalue?svalue[1]:svalue;
    }
}*/

function _buildMap(){
    _queryMap = {};

    var pos = -1;
    var currPageUrl = window.location.href;

    if((pos = currPageUrl.indexOf("?")) < 0)
        return;

    var queryString = decodeURIComponent(currPageUrl.substr(pos+1));
    queryString = queryString.replace(/#+$/,'');

    var hary = queryString.split(/=|&/);

    for(var k = 0;hary.length - k > 1;k += 2){
        var val = hary[k+1];
        if(val != undefined){
            if(/^[\d\.]+$/.test(val))
                if(val.indexOf(".")<0)
                    val = parseInt(val);
                else
                    val = parseFloat(val);
            if("true" == val || "false" == val)
                val= (val == "true");
        }
        _queryMap[hary[k]] = val;
    }
}

exports.getUrlVal = function(key){
    if('undefined' == typeof(_queryMap)){
        _buildMap();
    }
    return _queryMap[key];
};

exports.removeKeyFromUrl = function(url,key){
    if(!url || !key)
        return url;
    var regx = eval("/(\\?|&)" + key + "=[^&]+(&)?/ig");
    return url.replace(regx,function(match,c1,c2){
        if(c2 && "?" == c1)
            return "?";
        return "&";
    });
};

/**
 * 跳转事件模拟
 * */
exports.gotoPage = function(pageurl){
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent('click', true, true);
    $("<a/>").attr("href", pageurl).get(0).dispatchEvent(clickEvent);
};

/*广告位链接的处理 追加URL参数*/
exports.addUrlKey = function(orgUrl , kv){
    if(kv && typeof kv === "object") {
        var conn = '?';
        var k;
        var newKv = [];
        if(orgUrl.indexOf('?')>0)
            conn = '&';

        for(k in kv){
            if(kv.hasOwnProperty(k)){
                newKv.push([k, '=', kv[k]].join(''));
            }
        }
        if(newKv.length > 0)
            orgUrl += conn + newKv.join('&');
    }
    return orgUrl;
};



